<template>
  <div class="login-container">
    <!--登录框容器-->
    <div class="loginBgController">
      <img class="letfImg" src="@/assets/login/bg.png">
      <div class="formControlerDara">
        <div class="title">后台管理系统</div>
        <el-form
          ref="loginForm"
          label-position="top"
          label-width="80px"
          class="login-form"
          :model="loginForm"
          :rules="loginRules"
        >
          <el-form-item label="管理员账号">
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="Username"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>
          <el-form-item label="密码">
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="Password"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
          </el-form-item>

          <el-button
            round
            :loading="loading"
            type="primary"
            style="margin-top:49px; width: 100%;"
            @click.native.prevent="handleLogin"
          >登录</el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback()
        // callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码必须大于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              // 登陆默认跳转页面
              // this.$router.push({ path: this.redirect || '/userInfo/index' })
              this.$router.push({ path: '/userInfo/index' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

/* reset element-ui css */
.login-container {
  background-image: url("../../assets/login/left.png");
  background-position: center;

  .el-form--label-top .el-form-item__label {
    font-size: 16px !important;
    padding: 0px !important;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  min-width: 1200px;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  // 登陆框
  .loginBgController {
    background: #ffffff;
    width: 1020px;
    display: flex;
    border-radius: 10px;
    align-items: center;
    margin: auto;
    margin-top: 8%;
    .letfImg {
      border-top-left-radius: 10px;
      border-bottom-left-radius: 10px;
    }
  }
  .formControlerDara {
    flex: 1;
    padding: 0px 50px;
    transition: all 0.2s;

    .title {
      text-align: center;
      font-size: 36px;
      font-weight: 600;
    }
  }

  .login-form {
    margin-top: 45px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
